import axios from 'axios';
import Vue from 'vue';
import store from '../store/index';

// create an axios instance 创建axios实例
const service = axios.create({
    baseURL: process.env.VUE_APP_BaseURL, // url = base url + request url
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})

// request interceptor   请求拦截  AOP  过滤器
service.interceptors.request.use(
    config => {
        if (store.state.token) {
            config.headers['Authorization'] = `bearer ${Vue.ls.get("token")}`
        }
        return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)


export default service;